<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//前台控制器
class ListController extends Controller{
	public function index(){
		$cateModel=new \Common\Model\Cate;
		$arcModel=new \Common\Model\Arc;
		$tagModel=new \Common\Model\Tag;
		$arcTagModel = new \Common\Model\ArcTag;
		//点击分类
		if($cid = Q('get.cid',0,'intval')){
			$name = '分类';
			$value=$cateModel
				  ->where("cid={$cid}")
				  ->pluck('cname');
			//获取所有子集
			$cids=$cateModel->getSon($cateModel->get(),$cid);
			$cids[]=$cid;
			//查询文章表统计总数
			$total = $arcModel
			 	   ->where("category_cid IN(" . implode(',', $cids).")")
				   ->count();
			//查询分类对应的文章
			$data = $arcModel
				  ->join('category','category_cid','=','cid')
				  ->where("category_cid IN(". implode(',', $cids).")")
				  ->get();
			foreach ($data as $k => $v) {
			//因为打印$data时没有tag，所以要压入tag
			$data[$k]['tag']=$arcTagModel
							->join('tag','tag_tid','=','tid')
							->where("article_aid={$v['aid']}")
							->get();
			}
				 
		}
//		p($data);
		//点击标签
		if($tid=Q('get.tid',0,'intval')){
			$name='标签';
			$value=$tagModel
				  ->where("tid={$tid}")
				  ->pluck('tname');
			$total=$arcTagModel
				  ->where("tag_tid={$tid}")
				  ->count();
			//显示标签下的文章
			$data=$arcTagModel
				 ->join('article','article_aid','=','aid')
				 ->join('category','category_cid','=','cid')
				 ->where("tag_tid={$tid}")
				 ->get();
			foreach ($data as $k => $v) {
			//因为打印$data时没有tag，所以要压入tag
			$data[$k]['tag']=$arcTagModel
							->join('tag','tag_tid','=','tid')
							->where("article_aid={$v['aid']}")
							->get();
			}
		}		
		View::with('name',$name);
		View::with('value',$value);
		View::with('total',$total);
		View::with('data',$data);
		View::make();
	}



}



 ?>